package com.apes.fn.scm.retailOrder.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.mdm.paint.PaintBusinessScope;
import com.apes.fn.mdm.paint.PaintBusinessScopeRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.ReflectUtil;
import com.apes.scm.masterdata.product.model.Product;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wuq
 * @create 2020-07-31 11:14
 */
@Service
public class OrderPaintProductService extends DomainService {

    public <T> T setPaintInfo(String deptId, T item) {
        Product product = ReflectUtil.getFieldValue(item, "product");
        List<PaintBusinessScope> paintBusinessScopes = findPaintBusinessScope(deptId, product.getClassify().getId());
        if (paintBusinessScopes.size() == 0) return item;

        PaintBusinessScope paintBusinessScope = paintBusinessScopes.get(0);
        JSONObject paintPrice = getPaintPrice(product.getId(), paintBusinessScope);
        if (!paintPrice.containsKey("Q004") || !paintPrice.containsKey("Q003") || !paintPrice.containsKey("Q001") || !paintPrice.containsKey("Q002")) {
            throw new RuntimeException("未定义油漆相关价格策略，联系采购人员定义，商品编码为：" + product.getId());
        }

        // 免费标记字段控制
        if (!Objects.isNull(paintPrice.getString("Q005")) && "1".equals(paintPrice.getString("Q005"))) return item;

        if (paintPrice.getDouble("Q004") < 0.001d || paintPrice.getDouble("Q003") < 0.001d) {
            throw new RuntimeException("油漆分销价格为 0，联系采购人员定义，商品编码为：" + product.getId());
        }

        if (paintPrice.getDouble("Q001") < 0.001d || paintPrice.getDouble("Q002") < 0.001d) {
            throw new RuntimeException("油漆采购价格为 0，联系采购人员定义，商品编码为：" + product.getId());
        }

        // 设置价格
        setPrice(item, paintBusinessScope, paintPrice);

        ReflectUtil.setField(item, "paintFlag", true);
        ReflectUtil.setField(item, "supplier", paintBusinessScope.getSupplier());
        return item;
    }

    private <T> void setPrice(T item, PaintBusinessScope paintBusinessScope, JSONObject paintPrice) {
        /* 条件策略返回字段
            <Q001 name="油性采购价" nullValue="0.00"/>
            <Q002 name="水性采购价" nullValue="0.00"/>
            <Q003 name="油性分销价" nullValue="0.00"/>
            <Q004 name="水性分销价" nullValue="0.00"/>
            <Q005 name="免费标记" nullValue="0"/>
        */
        if ("water".equals(paintBusinessScope.getPaintType())) {
            ReflectUtil.setField(item, "paintPrice", paintPrice.getDouble("Q004")); // 获取销售价格
            ReflectUtil.setField(item, "paintPurchasePrice", paintPrice.getDouble("Q002"));    // 获取采购价格
            ReflectUtil.setField(item, "paintType", 0);
        } else {
            ReflectUtil.setField(item, "paintPrice", paintPrice.getDouble("Q003")); // 获取销售价格
            ReflectUtil.setField(item, "paintPurchasePrice", paintPrice.getDouble("Q001")); // 获取采购价格
            ReflectUtil.setField(item, "paintType", 1);
        }
    }

    private List<PaintBusinessScope> findPaintBusinessScope(String deptId, String classifyId) {
        return SpringManager.getBean(PaintBusinessScopeRepository.class).findByDeptAndClassify(deptId, classifyId);
    }

    private JSONObject getPaintPrice(String productId, PaintBusinessScope paintBusinessScope) {
        return newGetPaintPrice(productId, paintBusinessScope.getCompany().getId(),
                paintBusinessScope.getSupplier().getParty().getPatyRoleByType("Supplier").getId());
    }

    /**
     * 老切新之后调用获取价格
     *
     * @param productId product
     * @param companyId company
     * @return JSONObject
     */
    private JSONObject newGetPaintPrice(String productId, String companyId, String supplierId) {
        Map map = MapUtil.mapper("PRODUCT_ID", productId, "COMPANY_ID", companyId, "SUPPLIER_ID", supplierId, "SYS_RQ", new Date(), "conditionStrategyId", "CG50");
        Object obj = invoke("condition.execConditionStrategy", map);
        JSONObject result = new JSONObject(MapUtil.mapped(obj));
        return result;
    }

}
